package com.lb.bs.client.processor.handle.statics.second;

import com.lb.bs.client.annotation.ProcessorMark;
import com.lb.bs.client.annotation.SingletonMark;
import com.lb.bs.client.beans.GroupInfo;
import com.lb.bs.client.beans.LBGrayIPBean;
import com.lb.bs.client.beans.LBGrayItemBean;
import com.lb.bs.client.beans.LBItemBean;
import com.lb.bs.client.repository.ScanStaticModel;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.processor.ProcessorService;
import com.lb.bs.client.processor.enums.ProcessorType;
import com.lb.bs.client.util.CollectionUtils;
import com.lb.bs.client.util.KeyGenerateUtil;

import java.util.Collections;
import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-18 17:12
 * description:
 */
@SingletonMark
@ProcessorMark(type = ProcessorType.SECOND_INIT_GRAY_INIT)
public class StaticInitGrayInfoProcessor implements ProcessorService<ScanStaticModel> {

    @Override
    public void process(ScanStaticModel scanStaticModel) {
        //灰度放入空值
        StoreCenter storeCenter = StoreCenter.getInstance();
        Map<String, LBGrayIPBean> grayIPKeyMap = storeCenter.getGrayIPKeyMap();
        Map<String, GroupInfo> groupInfoMap = storeCenter.getGroupInfoMap();
        for (Map.Entry<String, GroupInfo> infoEntry : groupInfoMap.entrySet()) {
            String groupName = infoEntry.getKey();
            String grayIPKey = KeyGenerateUtil.spliceGrayIPKey(groupName);
            LBGrayIPBean ipBean = new LBGrayIPBean(groupName, Collections.emptyList());
            grayIPKeyMap.put(grayIPKey, ipBean);

        }

        Map<String, LBItemBean> itemKeyMap = storeCenter.getItemKeyMap();
        Map<String, LBGrayItemBean> grayItemKeyMap = storeCenter.getGrayItemKeyMap();
        if (CollectionUtils.isNotEmpty(itemKeyMap)) {
            for (Map.Entry<String, LBItemBean> entry : itemKeyMap.entrySet()) {
                LBItemBean itemBean = entry.getValue();
                if (itemBean == null) {
                    continue;
                }
                String grayItemKey = KeyGenerateUtil.spliceGrayItemKey(entry.getKey());
                LBGrayItemBean grayItemBean = new LBGrayItemBean(grayItemKey, null, itemBean.getGroupName());
                grayItemKeyMap.put(grayItemKey, grayItemBean);
            }
        }
    }
}
